package com.xoa.service.crm;

import com.xoa.dao.crm.CrmRoleMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.crm.CrmRole;
import com.xoa.model.users.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by qiyang on 2019/7/22.
 */
@Service
public class CrmRoleService {

    @Autowired
    private CrmRoleMapper crmRoleMapper;
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 删除编辑权限
     */
    public int setUpCrmRole(CrmRole crmRole){
        List<CrmRole> crmRoles = crmRoleMapper.selectAll();
        int i = 0;
        if(crmRoles.size() == 0){
            i = crmRoleMapper.insert(crmRole);
        }else {
            for (CrmRole cRole: crmRoles) {
                if(crmRole.getEditUser() != null && crmRole.getDelUser() != null) {
                    cRole.setDelUser(crmRole.getDelUser());
                    cRole.setEditUser(crmRole.getEditUser());
                }else if(crmRole.getEditUser() != null) {
                    cRole.setEditUser(crmRole.getEditUser());
                }else if(crmRole.getDelUser() != null){
                    cRole.setDelUser(crmRole.getDelUser());
                }
                 i = crmRoleMapper.updateByPrimaryKeySelective(cRole);
            }

        }
        return i;
    }

    /**
     * 查询所有扁家删除权限人
     */
    public List<CrmRole> selectAll(){
        List<CrmRole> crmRoles = crmRoleMapper.selectAll();
        for (CrmRole role:crmRoles) {
            //编辑权限
            String editUser = role.getEditUser();
            StringBuffer stringBufferedit = new StringBuffer();
            if(editUser.contains(",")){
                String[] split = editUser.split(",");
                for (int i=0;i<split.length;i++){
                    String username = usersMapper.getUsernameByUserId(split[i]);
                    //判断是否为最后一个
                    if(i == split.length-1){
                        stringBufferedit.append(username);
                    }else {
                        stringBufferedit.append(username+",");
                    }
                }
                role.setEditUserName(String.valueOf(stringBufferedit));
            }

            //删除权限
            String delUser = role.getDelUser();
            StringBuffer stringBufferDel = new StringBuffer();
            if(delUser.contains(",")){
                String[] split = delUser.split(",");
                for (int i=0;i<split.length;i++){
                    String username = usersMapper.getUsernameByUserId(split[i]);
                    //判断是否为最后一个
                    if(i == split.length-1){
                        stringBufferDel.append(username);
                    }else {
                        stringBufferDel.append(username+",");
                    }
                }
                role.setDelUserName(String.valueOf(stringBufferDel));
            }
        }
        return crmRoles;
    }
}
